﻿using System;
using System.Data;
using System.Windows.Forms;
using template_dll;
using System.Data.SqlClient;

namespace ServerClient
{
    public partial class FoldersForm : Form
    {
        public FoldersForm()
        {
            InitializeComponent();
        }

        private SqlConnection con = new SqlConnection(@"Server=sdata2;Database=ExchangeFilesDB;Integrated Security=True");

        private void FoldersForm_Load(object sender, EventArgs e)
        {
            PathsSel();
        }

        public void PathsSel()
        {

            try
            {
                SPparams[] sp = new SPparams[0];
                if (textBox1.Text != "")
                {
                    Array.Resize(ref sp, sp.Length + 1);
                    sp[sp.Length - 1] = new SPparams() { Name = "@client", Value = textBox1.Text };
                }
                if (textBox2.Text != "")
                {
                    Array.Resize(ref sp, sp.Length + 1);
                    sp[sp.Length - 1] = new SPparams() { Name = "@path", Value = textBox2.Text };
                }
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = new SqlStorProc().SqlProcExecWithQueryOneTable(con, "PathSel", 30, sp).DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (con.State != ConnectionState.Closed) con.Close();
            }
        }

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) PathsSel();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PathsSel();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("Всего будет удалено строк: " + dataGridView1.SelectedRows.Count + "\nПродолжить?", "Удаление строк", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    SPparams[] sp = new SPparams[1];
                    sp[0] = new SPparams() { Name = "@pathid" };
                    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                    {
                        sp[0].Value = row.Cells[0].Value;
                        new SqlStorProc().SqlProcExecNonQuery(con, "PathDel", 30, sp);
                    }
                    PathsSel();
                }
            }
            else MessageBox.Show("Не выбраны строки");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            InsNewPath f = new InsNewPath();
            if (f.ShowDialog() == DialogResult.OK) PathsSel(); 
            f.Close();
            f = null;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                InsNewPath f = new InsNewPath();
                if (dataGridView1[5, e.RowIndex].Value.ToString() == "нет") f.forAll = false;
                else f.forAll = true;
                if (dataGridView1[6, e.RowIndex].Value.ToString() == "нет") f.map = false;
                else f.map = true;
                if (dataGridView1[1, e.RowIndex].Value != DBNull.Value) f.client = Convert.ToInt32(dataGridView1[1, e.RowIndex].Value);
                if (dataGridView1[4, e.RowIndex].Value.ToString() == "входящие файлы") f.inout = "in";
                else f.inout = "out";
                if (dataGridView1[3, e.RowIndex].Value.ToString() != "") f.path = dataGridView1[3, e.RowIndex].Value.ToString();
                f.pathId = Convert.ToInt32(dataGridView1[0, e.RowIndex].Value);
                if (f.ShowDialog() == DialogResult.OK) PathsSel();
                f.Close();
                f = null;
            }
        }
    }
}
